package com.jwgf.group;


import androidx.annotation.NonNull;

import com.jwgf.objectbox.ObjectBox;
import com.jwgf.objectbox.SpindleParameter;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import io.objectbox.Box;

public class Group implements Comparable<Group>, Serializable {
    private List<Integer> spindles;
    private long id;

    public Group() {
    }

    public Group(List<Integer> spindles, long id) {
        this.spindles = spindles;
        this.id = id;
    }

    public Group(int start, int end, long id) {
        List<Integer> list = new ArrayList<>();
        for (int i = start; i <= end; ++i) {
            list.add(i);
        }
        this.spindles = list;
        this.id = id;
    }

    public void setSpindles(List<Integer> spindles) {
        this.spindles = spindles;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public List<Integer> getSpindles() {
        return spindles;
    }

    public int getStartSpindle() {
        return spindles.get(0);
    }

    public int getEndSpindle() {
        return spindles.get(spindles.size() - 1);
    }

    @Override
    public int compareTo(@NonNull Group o) {
        return spindles.get(0) - o.getSpindles().get(0);
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("\t\t");
        sb.append("sp");
        sb.append("[");
        sb.append(String.format(Locale.US, "%02d", getStartSpindle()));
        sb.append("-");
        sb.append(String.format(Locale.US, "%02d", getEndSpindle()));
        sb.append("]");
        sb.append("\t\t");
        Box<SpindleParameter> box = ObjectBox.get().boxFor(SpindleParameter.class);
        sb.append(box.get(id).craftName);
        return sb.toString();
    }

}
